package cn.cmdr.controller;

import cn.cmdr.pojo.Journal;
import cn.cmdr.pojo.Order;
import cn.cmdr.pojo.OrderItem;
import cn.cmdr.pojo.Staff;
import cn.cmdr.service.JournalService;
import cn.cmdr.service.OrderItemService;
import cn.cmdr.service.OrderService;
import cn.cmdr.vo.ItemVo;
import cn.cmdr.vo.OrderVo;
import cn.cmdr.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ord/order")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private JournalService journalService;
    @PostMapping("/orderList")
    public ResultVo getOrderList(){
        List<Order> list = orderService.getOrderList();
        return ResultVo.sucess(list);
    }
    @PostMapping("/list/{pageSize}/{pageNo}")
    public ResultVo getList(@PathVariable Integer pageSize, @PathVariable Integer pageNo,@RequestBody OrderVo orderVo){
        return ResultVo.sucess(orderService.getPageOrderList(pageNo,pageSize,orderVo));
    }
    @GetMapping("/view")
    public ModelAndView view(String id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("ord/orderview");
        modelAndView.addObject("order",orderService.getOrderById(Integer.parseInt(id)));
        return modelAndView;
    }
    @GetMapping("/item")
    public ResultVo getOrderItem(String orderId){
        List<OrderItem> list = orderItemService.getOrdItem(Integer.parseInt(orderId));
        return ResultVo.sucess(new ItemVo(list,list.size()));
    }
    @PostMapping("/upItem/{orderId}")
    @RequiresPermissions("order:update")
    public ResultVo upItem(@PathVariable Integer orderId,@RequestBody Order order){
        Order order2 =orderService.getOrderById(orderId);
        if (order2.getStatus()==1){
//            order2.setDeliverySn(order.getDeliverySn());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            int random=(int)(Math.random()*10);
            String sn = sdf.format(new Date())+random;
            order2.setDeliverySn(sn);
            order2.setDeliveryCompany(order.getDeliveryCompany());
            order2.setStatus(2);
            order2.setDeliveryTime(new Date());
            try {
                orderService.updateOrder(order2);
                Journal journal = new Journal();
                journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
                journal.setUpDate(new Date());
                journal.setUpContent("修改订单:"+order2.getOrderSn());
                journal.setMenuId(4);
                journalService.add(journal);
                return ResultVo.sucess("修改成功");
            }catch (Exception e){
                return ResultVo.fail("修改失败",e);
            }
        }
        return ResultVo.fail("失败");
    }
    @GetMapping("/del")
    @RequiresPermissions("order:del")
    public ResultVo delete(String id){
        try {

            Order order2 =orderService.getOrderById(Integer.parseInt(id));
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("删除订单:"+order2.getOrderSn());
            journal.setMenuId(4);
            orderService.delOrder(Integer.parseInt(id));
            journalService.add(journal);
            return ResultVo.sucess("删除成功");
        }catch (Exception e){
            return ResultVo.fail("删除失败",e);
        }
    }

}
